package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringGoodsDao;
import com.catering.dao.CateringSpacesChildDao;
import com.catering.dao.CateringSpacesDao;
import com.catering.entity.CateringGoods;
import com.catering.entity.CateringSpaces;
import com.catering.entity.CateringSpacesChild;
import com.catering.entity.vo.CateringSpacesVo;
import com.catering.service.CateringGoodsService;
import com.catering.service.CateringSpacesChildService;
import com.catering.service.CateringSpacesService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CateringSpacesServiceImpl extends ServiceImpl<CateringSpacesDao, CateringSpaces> implements CateringSpacesService {
	
	@Autowired
	private CateringSpacesDao cateringSpacesDao;
	@Autowired
	private CateringSpacesChildDao cateringSpacesChildDao;
	@Autowired
	private CateringGoodsDao cateringGoodsDao;
	@Autowired
	private CateringGoodsService cateringGoodsService;
	@Autowired
	private CateringSpacesChildService spacesChildService;

	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper<CateringSpaces> wrapper = CommonUtils.getKeySet(map,"");
			List<CateringSpaces> list = new ArrayList<CateringSpaces>();
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringSpaces> page = new Page<>(Integer.parseInt(map.get("page")),Integer.parseInt(map.get("limit")));
				IPage<CateringSpaces> pageList = cateringSpacesDao.selectPage(page, wrapper);
				Integer count = cateringSpacesDao.selectCount(wrapper);
				list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				list = cateringSpacesDao.selectList(wrapper);
				return ResultData.success(list);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData selectSpaces(Map<String,String> map) {
		try {
			QueryWrapper<CateringSpaces> wrapper = CommonUtils.getKeySet(map,"t1");
			wrapper.orderByAsc("t1.sort");
			wrapper.eq("t1.status",0);
			List<CateringSpaces> list = cateringSpacesDao.selectNoPageList(wrapper);
			List<String> ids = list.stream().map(CateringSpaces::getSysId).collect(Collectors.toList());
			List<CateringSpacesChild> childList = cateringSpacesChildDao.selectList(new LambdaQueryWrapper<CateringSpacesChild>().in(CateringSpacesChild::getParentId,ids).eq(CateringSpacesChild::getStatus,0));
			List<CateringSpacesVo> voList = new ArrayList<>();
			for(CateringSpaces spaces : list){
				CateringSpacesVo vo = JSON.parseObject(JSON.toJSONString(spaces), CateringSpacesVo.class);
				List<CateringSpacesChild> childList1 = new ArrayList<>();
				for(CateringSpacesChild child : childList){
					if(spaces.getSysId().equals(child.getParentId())){
						childList1.add(child);
					}
				}
				vo.setChildList(childList1);
				voList.add(vo);
			}
			return ResultData.success(voList);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringSpaces vo = JSON.parseObject(JSON.toJSONString(map),CateringSpaces.class);
			if(CommonUtils.checkNoNull(vo.getSpacesName())) {
				CateringSpaces bt = cateringSpacesDao.selectOne(new LambdaQueryWrapper<CateringSpaces>().eq(CateringSpaces::getSpacesName, vo.getSpacesName()).eq(CateringSpaces::getGoodsId, vo.getGoodsId()));
				if(bt != null) return ResultData.error(ResultEnum.SAME_DATA.getCode(),"规格名称“"+vo.getSpacesName()+"”"+ResultEnum.SAME_DATA.getMsg());
			}else return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),"规格名称"+ResultEnum.REQ_DATA_ERROR.getMsg());
			vo.setCreateTime(CommonUtils.getTime(19));
			vo.setSysId(CommonUtils.getUuid());
			int flag = cateringSpacesDao.insert(vo);
			if(flag > 0) {
				CateringGoods goods = cateringGoodsDao.selectById(vo.getGoodsId());
				goods.setIsSpaces(1);
				cateringGoodsService.updateById(goods);
			}
			List<CateringSpacesChild> childList = new ArrayList<>();
			if(vo.getChildList() != null && vo.getChildList().size() > 0){
				for(CateringSpacesChild child : vo.getChildList()){
					child.setSysId(CommonUtils.getUuid());
					child.setParentId(vo.getSysId());
					child.setCreateTime(CommonUtils.getTime(19));
					childList.add(child);
				}
				spacesChildService.saveBatch(childList);
			}
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringSpaces vo = JSON.parseObject(JSON.toJSONString(map),CateringSpaces.class);
			CateringSpaces as = cateringSpacesDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
			int flag = cateringSpacesDao.updateById(vo);
			if(flag > 0) {
				cateringSpacesChildDao.delete(new LambdaQueryWrapper<CateringSpacesChild>().eq(CateringSpacesChild::getParentId,vo.getSysId()));
				CateringGoods goods = cateringGoodsDao.selectById(vo.getGoodsId());
				goods.setIsSpaces(1);
				cateringGoodsService.updateById(goods);
			}
			List<CateringSpacesChild> childList = new ArrayList<>();
			if(vo.getChildList() != null && vo.getChildList().size() > 0){
				for(CateringSpacesChild child : vo.getChildList()){
					child.setSysId(CommonUtils.getUuid());
					child.setParentId(vo.getSysId());
					child.setCreateTime(CommonUtils.getTime(19));
					childList.add(child);
				}
				spacesChildService.saveBatch(childList);
			}
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,Object> map) {
		try {
			CateringSpaces vo = JSON.parseObject(JSON.toJSONString(map),CateringSpaces.class);
			CateringSpaces as = cateringSpacesDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
			int flag = cateringSpacesDao.deleteById(as.getSysId());
			if(flag > 0) {
				cateringSpacesChildDao.delete(new LambdaQueryWrapper<CateringSpacesChild>().eq(CateringSpacesChild::getParentId,vo.getSysId()));
				List<CateringGoods> goodsList = cateringGoodsDao.selectList(new LambdaQueryWrapper<CateringGoods>().eq(CateringGoods::getSysId,as.getGoodsId()));
				if(goodsList == null || goodsList.isEmpty()) {
					CateringGoods goods = cateringGoodsDao.selectById(as.getGoodsId());
					goods.setIsSpaces(0);
					cateringGoodsDao.updateById(goods);
				}
			}
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
